/* suppose you have to add two big numbers (may be more than 50 digits)
 * in that case normal addition will won't work as long and long long
 * int have their limits.
 * Here I present my solution to this problem.
 * This program is still in progress.
*/

#include<iostream>
#include<math.h>
using namespace std;

class BigNum
{
	int *p, *q, len;	// p and q to save big numbers
	
	// to convert and save the big numbers in arrays
	void basic(string a, string b, int length)
	{	
		int k = a.length()>b.length() ? a.length() : b.length();
		k = k%length ? k+length-k%length : k;
		p = new int[k/length];
		q = new int[k/length];
		int i = k-a.length();
		
		while(i)
		{
			a = "0"+a;
			i--;	
		}
		
		i = k-b.length();
		
		while(i)
		{
			string temp = "0";
			b = temp.append(b);
			i--;
		}
		
		i = k/length;
		
		while(i)
		{
			p[i-1] = q[i-1] = 0;
			i--;
		}
		
		for(int i=0; i<k/length; i++)
		{
			for(int j=i*length; j<i*length+length; j++)
			{
				p[i] = 10*p[i] + int(a[j]) - int('0');
				q[i] = 10*q[i] + int(b[j]) - int('0');
			}
		}
		len = k/length;
	}
	
	public:
	
	void add(string a, string b,int length = 9)
	{
		while(length>9)
		{
			cout << "Chutiye length fir se de, itni badi support nahin karege mera programme" << endl;
			cin >> length;
		}
		
		basic(a,b,length);	// to save a and b in int arrays
		
		int *r = new int[len+1];
		int i = len+1;
		
		while(i)
		{
			r[i-1] = 0;
			i--;
		}
		
		i = len;
		int carry = 0;
		int higlim = pow(10,length);
		
		//performing the final addition in array r[]
		while(i)
		{
			int temp = p[i-1]+q[i-1]+carry;
			r[i] = temp%higlim;
			carry = temp/higlim;
			i--;
		}
		
		r[i] = carry;
		
		//print the solution
		for(int i=0; i<=len; i++)
		{
			int temp = r[i] ? length-1 -int(log(r[i])/log(10)) : length-1;
			while(temp>0)
			{
				cout << 0;
				temp--;
			}
			cout << r[i];
		}
		
		cout << endl;
		delete []p;
		delete []q;
		return;
	}
	
	// for multiplication
	void multi(string a, string b, int length = 4)
	{
		while(length>4)
		{
			cout << "Chutiye length fir se de, itni badi support nahin karege mera programme" << endl;
			cin >> length;
		}
		
		basic(a,b,length);
		
		int result = a.length()+b.length();
		result = result%4 ? result + 4 - result%4 : result;
		int *r = new int[result/4];
		int i;
		int limit = pow(10,length);
		result = result/4;
		int sum = 0, carry = 0;
		
		
		for(i=0; i<len; i++)
		{
			sum = carry;
			carry = 0;
			
			for(int x = 0; x<=i; x++)
			{
				sum = sum + p[len-1-x]*q[len-1-i+x];
				carry = carry + sum/limit;
				sum = sum%limit;
			}
			
			r[result-1-i] = sum;
		}
		
		while(i<result)
		{
			sum = carry;
			carry = 0;
			for(int x = i+1-len; x < len; x++)
			{
				sum = sum + p[len-1-x]*q[len-1-i+x];
				carry = carry + sum/limit;
				sum = sum%limit;
			}
			
			r[result-1-i] = sum;
			i++;
		}
		
		//print the result.
		for(int i=0; i<result; i++)
		{
			int temp = r[i] ? length-1 -int(log(r[i])/log(10)) : length-1;
			while(temp>0)
			{
				cout << 0;
				temp--;
			}
			cout << r[i];
		}
		cout << endl;
		
		delete []p;
		delete []q;
		delete []r;
	}
};


int main()
{
	string i,j;
	cout << "Enter numbers seperately by space or return" << endl;
	cin >> i >> j;
	BigNum B;
//	B.add(i,j);
//	B.multi(i,j)

	cout << "Enter the Operation, +,-,*,/" << endl;
	char c;
	cin >> c;
	switch(c)
	{
		case '+':
			cout << "Sum of two numbers is:" << endl;
			B.add(i,j);
			break;
			
/*		case '-':
			r = B.sub();
*/			break;
		
		case '*':
			cout << "Multiplication of both numbers is:" << endl;
			B.multi(i,j);
			break;
		
/*		case '/':
			r = B.div();
			break;
*/			
		default:
			cout << "Wrong Operation gadhe, fir se run kar program" << endl;
			break;
	}		
	
			
	cout << "Baklol" << endl;
	return 0;
}

